package org.brain.richfaces;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.brain.dbo.DBOperate;
import org.brain.pojos.Treemenu;
import org.hibernate.dialect.DB2390Dialect;
import org.richfaces.component.UITree;
import org.richfaces.component.html.HtmlTree;
import org.richfaces.event.NodeSelectedEvent;
import org.richfaces.model.TreeNode;
import org.richfaces.model.TreeNodeImpl;

public class TreeBean {
    
    private NewTreeNode rootNode = null;
    private List<String> selectedNodeChildren = new ArrayList<String>();    
    
    private String nodeTitle;
    //private static final String DATA_PATH = "treedate.properties";
    
    private void addNodes(int fatherNode,String userName,NewTreeNode node) {
    		
    	List<Treemenu>  list = DBOperate.getTreeMenu(userName, fatherNode);
    	if(list!=null)
    	{
    		for(Treemenu tree:list)
    		{
    		   NewTreeNode leafNode =  new NewTreeNode();
    		   leafNode.setData(tree.getNodename());
    		   node.addChild(tree.getNodeid(), leafNode);
    		   
    		   System.out.println(tree.getNodename());
    		   if(tree.getChildnode()!=0)
    		   {
    			   addNodes(tree.getNodeid(),userName,leafNode);
    		   }
    		}
    	}
    	
    }
    
    private void loadTree() {
    	
    	try{
    		
         rootNode = new NewTreeNode();
       	 NewTreeNode treeRoot = null; 
       	 treeRoot = new NewTreeNode();   	 
       	 treeRoot.setData("题录");
       	 rootNode.addChild(0, treeRoot);
       	 addNodes(0, "liufanke", treeRoot);
       	 
    	}catch(Exception e)
    	{
    		e.getMessage();
    	}   	
    	
    }
    
    public void processSelection(NodeSelectedEvent event) {
        HtmlTree tree = (HtmlTree) event.getComponent();
    	//NewTreeNode tree = event.getComponent();
       
        nodeTitle = (String) tree.getRowData();
        //nodeTitle =  tree.getRowKeyVar();
        
        
        
      
       /* if (currentNode.isLeaf()){
            selectedNodeChildren.add((String)currentNode.getData());
        }else
        {
            Iterator<Map.Entry<Object, TreeNode>> it = currentNode.getChildren();
            while (it!=null &&it.hasNext()) {
                Map.Entry<Object, TreeNode> entry = it.next();
                selectedNodeChildren.add(entry.getValue().getData().toString()); 
            }
        }*/
    	
    
    }
    
    public NewTreeNode getTreeNode() {
        if (rootNode == null) {
            loadTree();
        }
        
        return rootNode;
    }


    
    public String getNodeTitle() {
        return nodeTitle;
    }

    public void setNodeTitle(String nodeTitle) {
        this.nodeTitle = nodeTitle;
    }

	public NewTreeNode getRootNode() {
		return rootNode;
	}

	public void setRootNode(NewTreeNode rootNode) {
		this.rootNode = rootNode;
	}

	public List<String> getSelectedNodeChildren() {
		return selectedNodeChildren;
	}

	public void setSelectedNodeChildren(List<String> selectedNodeChildren) {
		this.selectedNodeChildren = selectedNodeChildren;
	}
    
}